package com.example.javabasic.common.data;

import java.io.Serializable;

/**
 * @author: cph
 * @date: 2022/2/15 11:39
 */
public class ResultData<T> implements Serializable {

    private static final long serialVersionUID = -8458506836382280326L;
    private boolean success;
    private T data;
    private Integer retcode;
    private String retdesc;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getRetcode() {
        return retcode;
    }

    public void setRetcode(Integer retcode) {
        this.retcode = retcode;
    }

    public String getRetdesc() {
        return retdesc;
    }

    public void setRetdesc(String retdesc) {
        this.retdesc = retdesc;
    }

    public ResultData() {
    }

    private ResultData(boolean success, StatusCode statusCode) {
        this.success = success;
        this.retcode = statusCode.getCode();
        this.retdesc = statusCode.getDesc();
    }

    private ResultData(StatusCode statusCode, T data) {
        this.success = true;
        this.retcode = statusCode.getCode();
        this.retdesc = statusCode.getDesc();
        this.data = data;
    }

    private ResultData(boolean success, StatusCode statusCode, T data) {
        this.success = success;
        this.retcode = statusCode.getCode();
        this.retdesc = statusCode.getDesc();
        this.data = data;
    }

    public static <T> ResultData<T> success() {
        return new ResultData(true, StatusCodeEnum.SUCCESS);
    }

    public static <T> ResultData<T> success(T data) {
        return new ResultData(StatusCodeEnum.SUCCESS, data);
    }

    public static <T> ResultData<T> failure() {
        return new ResultData(false, StatusCodeEnum.FAILURE);
    }

    public static <T> ResultData<T> failure(StatusCode statusCode) {
        return new ResultData(false, statusCode);
    }

    public static <T> ResultData<T> failure(StatusCode statusCode, T data) {
        return new ResultData(false, statusCode, data);
    }

}
